
package org.argeproje.resim.ui.model.commands;

import org.argeproje.resim.ui.model.Shape;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ChangeBoundsRequest;


/**
 * A command to move a shape. The command can be undone or redone.
 * 
 */
public class ShapeSetConstraintCommand extends Command {
	/** Stores the new location. */
	private final Point newLocation;
	/** Stores the old location. */
	private Point oldLocation;
	/** A request to move/resize an edit part. */
	private final ChangeBoundsRequest request;

	/** Shape to manipulate. */
	private final Shape shape;

	/**
	 * Create a command that can move a shape.
	 * 
	 * @param shape
	 *            the shape to manipulate
	 * @param req
	 *            the move request
	 * @param newBounds
	 *            the new location
	 * @throws IllegalArgumentException
	 *             if any of the parameters is null
	 */
	public ShapeSetConstraintCommand(Shape shape, ChangeBoundsRequest req,
			Rectangle newBounds) {
		if (shape == null || req == null || newBounds == null) {
			throw new IllegalArgumentException();
		}
		this.shape = shape;
		this.request = req;
		this.newLocation = newBounds.getCopy().getLocation();
		setLabel("move");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	public boolean canExecute() {
		Object type = request.getType();
		// make sure the Request is of a type we support:
		/*
		 * return (RequestConstants.REQ_MOVE.equals(type) ||
		 * RequestConstants.REQ_MOVE_CHILDREN.equals(type) ||
		 * RequestConstants.REQ_RESIZE.equals(type) ||
		 * RequestConstants.REQ_RESIZE_CHILDREN.equals(type));
		 */
		// only move and move children
		return RequestConstants.REQ_MOVE.equals(type)
				|| RequestConstants.REQ_MOVE_CHILDREN.equals(type);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		oldLocation = new Point(shape.getLocation());
		redo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		// shape.setSize(newBounds.getSize());
		shape.setLocation(newLocation);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		shape.setLocation(oldLocation);
	}
}
